#ifndef __ILIB_CORE_CONNECTION_H_20120323__
#define __ILIB_CORE_CONNECTION_H_20120323__

#include "iSocket.h"
#include "iTPConnection.h"
#include "../util/h/iMsg.h"

#define DEBUG(x) iDEBUG(MOD_TP,x)
#define FUNC(x)  iFUNC(MOD_TP,x)
#define INFO(x)  iINFO(MOD_TP,x)
enum
{
    MSG_TP_CONNECTION_IN    =2012,
    MSG_TP_CONNECTION_OUT   ,
    MSG_TP_DATA_IN          ,
    MSG_TP_DATA_OUT         ,
    MSG_TP_CONNECTION_TERM  ,
    MSG_TP_LISTEN           ,
	MSG_TP_CLOSE			,
};

class iConnection : public iAllocator::Object
	              , public iSocket::iSocketSink
				  , public iMsg::iSink
{
public:
	enum eConnectionStatus { CS_UNSPEC, CS_OUT_GOING, CS_WAIT_REQ, CS_WAIT_LOCAL_RSP, CS_WAIT_REMOTE_RSP, CS_ESTABLISHED, CS_BROKEN };
	iConnection(iConnection* prvd, iAllocator* alloc);

public:
	// override iSocket::iSocketSink
	virtual void on_connect_confirm();	
	virtual void on_connect_indication(iClientSocket* cs);
	virtual void on_data_indication(iMsgDB* mdb);
    virtual void on_write();
	virtual void on_close();

	// override iMsgProcessor
	virtual int on_message(iMsgID msg, void* param);

	// native
	inline void welding(iMsgThread* jobber, iMsgThread* worker) { m_jobber=jobber; m_worker=worker; }
	inline void release() {m_allocator->deallocate(this); }
    inline bool is_server() { return m_provider!=NULL; }

protected:
	iSocket*			m_socket;
	iMsgThread*			m_jobber;
	iMsgThread*			m_worker;
	iConnection*		m_provider;
	eConnectionStatus	m_conn_status;
    std::list<iMsgDB*>  m_data_q;
};

#endif  //__ILIB_CORE_CONNECTION_H_20120323__
